package com.ruoyi.index.userController;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.index.domain.WeatherDto;
import com.ruoyi.tokenTools.UserLoginToken;
import com.ruoyi.tools.HttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api( tags = "获取天气接口")
@RestController
@RequestMapping("/app/weather")
public class WeatherController {



    /**
     * 获取天气
     */
    @UserLoginToken
    @ApiOperation("获取天气")
    @GetMapping()
    public R<WeatherDto> list(String lat,String lon) {
        if(lat.equals("0.0")){
            WeatherDto weatherDto = new WeatherDto();
            return R.ok(weatherDto);
        }
        if(lat==null||lon==null){
            return R.fail("参数错误");
        }
        String appcode = "757a21742e06444b8aa3b7393e802f48";
        String host = "https://weather01.market.alicloudapi.com";
        String path = "/gps-to-weather";
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("from", "5");
        querys.put("lng", lon);
        querys.put("lat", lat);
        querys.put("needMoreDay", "0");
        querys.put("needIndex", "0");
        querys.put("needHourData", "0");
        querys.put("need3HourForcast", "0");
        querys.put("needAlarm", "0");

        try {
            //发送请求
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取响应实体
            HttpEntity entity = response.getEntity();
            //确保响应实体不为空
            if(entity!=null){
                //将响应实体转换为字符串
                String jsonString = EntityUtils.toString(entity);
                Gson gson = new Gson();
                //解析JSON字符串为JsonObject
                JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
                //提取值
                JsonObject showapiResBody = jsonObject.get("showapi_res_body").getAsJsonObject();
                JsonObject now = showapiResBody.get("now").getAsJsonObject();
                //天气
                String weather = now.get("weather").getAsString();
                //温度
                String temperature = now.get("temperature").getAsString();
                //湿度
                String sd = now.get("sd").getAsString();

                JsonObject f1 = showapiResBody.get("f1").getAsJsonObject();
                //日出时间，日落时间
                String sunBeginEnd = f1.get("sun_begin_end").getAsString();

                WeatherDto weatherDto = new WeatherDto(weather, temperature, sd,sunBeginEnd);

//                System.out.println(EntityUtils.toString(response.getEntity()));
                return R.ok(weatherDto);
            }
            return R.fail("获取天气失败");
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }

    }


}
